package com.lions.client.model;

/**
 * Call this list through Model. This list holds all of the modules.
 * 
 * @author Brad
 * 
 */
public class ModuleList extends java.util.LinkedList<Module> {

	/**
	 * Default Constructor Sets all of its variables to false
	 */
	public ModuleList() {
		super();
	}

	/**
	 * Copy constructor ModuleArray.
	 * Copies the elements in a new list. New list
	 * may not be sorted.
	 * @param moduleArray type list
	 */
	public ModuleList(final ModuleList moduleArray) {
		super();
		for (Module e : moduleArray) {
			if (e != null) {
				this.add(e);
			}
		}
	}

	/**
	 * Copy constructor primitive array.
	 * Copies the elements in a new list.
	 * New list may not be sorted.
	 * @array primArray arrays
	 */
	public ModuleList(final Module[] primArray) {

		int offset = 0;
		for (int i = 0; i < primArray.length; i++) {
			if (primArray[i] != null) {
				this.add(primArray[i - offset]);
			} else {
				offset++;
			}
		}
	}

	public int getModuleIndex(final Module m) {
		int index = -1;
		for (int i = 0; i < this.size(); i++) {
			if (m.equals(this.get(i))) {
				index = i;
			}
		}
		return index;
	}

	public Module getModuleAtCoord(final int x, final int y) {
		Module mod = null;
		for (Module m : this) {
			if ((m.getX() == x) && (m.getY() == y)) {
				mod = m;
			}
		}
		return mod;
	}

	public boolean removeModuleAtCoord(final int x, final int y) {
		boolean removed = false;
		for (Module m : this) {
			if ((m.getX() == x) && (m.getY() == y)) {
				removed = this.removeModule(m);
			}
		}
		return removed;
	}

	private boolean removeModule(final Module m) {
		boolean removed = false;
		for (int i = 0; i < this.size(); i++) {
			if ((m.getX() == this.get(i).getX())
					&& (m.getY() == this.get(i).getY())) {
				this.remove(i);
			}
		}
		return removed;
	}

}
